﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace Communication
{
    public class DeliveryCupControl
    {
        public static short BeltMotorID = 85;//传送带电机id
        public static short HookMotorID = 81;//勾电机id
        public static short CarMotorID = 80;//小车电机id
        public static short PoleMotorID = 79;//推杆电机id

        public const short RBeltMotorID = 149; //Convert.ToInt16 (BeltMotorID + 64 );//传送带电机id
        public const short RHookMotorID = 145;//Convert.ToInt16(HookMotorID + 64);//勾电机id
        public const short RCarMotorID = 144;//Convert.ToInt16(CarMotorID + 64);//小车电机id
        public const short RPoleMotorID = 143;// Convert.ToInt16(PoleMotorID + 64);//推杆电机id

        public static int BeltMotorStatus = 0;
        public static int PoleMotorStatus = 0;
        public static int CarMotorStatus = 0;
        public static int HookMotorStatus = 0;

        public static int status = -1;
        public static bool IsPause = false; //小车暂停标志位

        public static int FirstDetectPosition = 7875; // 第一个检测位的步数  // 更改前7875
        public static int y_OffsetDetect = 2480;  // 检测位之间的间距

        public DeliveryCupControl()
        {
        }
        /// <summary>
        /// 等待状态改变（等待某个命令的反馈）
        /// </summary>
        /// <param name="status">状态</param>
        public static void WaitStatus(int mCanId, ref int status)
        {
            int time = 0;
            while (status == 0)
            {
                if (time == 30000)
                {
                    Console.WriteLine(mCanId + "," + "超时");
                    //mSendBuffer.Add(mCmdModelDic[status]);

                    //throw new Exception("超时");
                }
                else if (time == 60000)
                {
                    //mIsTimeout = true;
                    //if (Application.Current != null)
                    //{
                    //    //Application.Current.Dispatcher.Invoke(new Action(() =>
                    //    //{
                    //    //    throw new Exception("fdfsdf");
                    //    //}));
                    //}
                    // MessageBox.Show(mCanId + ","  + "超时");
                    break;
                }
                System.Threading.Thread.Sleep(100);
                time += 100;
            }

            //while (mStatus != status)
            //{
            //    if (time > 1000)
            //    {
            //        break;
            //    }
            //    Thread.Sleep(100);
            //    time += 100;
            //}

            //暂停
            //while (IsPause)
            //{
            //    Thread.Sleep(100);
            //}

        }

        /// <summary>
        /// 复位操作
        /// </summary>
        public static void AllReset()
        {
            //皮带后退一点距离
            BeltComebackALittle();

            HookMotorStatus = 0;
            PoleMotorStatus = 0;

            ResetPushPole(); //复位推杆
            ResetHook();     //复位钩子

            WaitStatus(PoleMotorID, ref PoleMotorStatus);//检查推杆是否复位完成

            //小车复位之前先走一点
            CarMotorStatus = 0;
            CarComeBackALittle();
            WaitStatus(CarMotorID, ref CarMotorStatus);//检查小车是否停止

            CarMotorStatus = 0;
            ResetCar();//复位小车

            WaitStatus(HookMotorID, ref HookMotorStatus);//检查钩子是否复位
            WaitStatus(CarMotorID, ref CarMotorStatus);//检查小车是否复位


            Can.SendCan(80, "1IF" + "\r");
            //小车弹簧松开

            //Can.SendCan(80, "1VE5" + "\r");
            //Can.SendCan(80, "1DZ-700" + "\r");
            //Can.SendCan(80, "1FZ" + "\r");

            status = 0; //置状态为0 复位完成
        }
        public static void AllResetCleanCup()
        {


            CarMotorStatus = 0;
            CarComeBackToReceiveCup();//车对齐
            WaitStatus(CarMotorID, ref CarMotorStatus);//等待车完成
            HookMotorStatus = 0;
            HookPushCupToCar();//钩子推杯上车
            WaitStatus(HookMotorID, ref HookMotorStatus); //等待钩子到位
            BeltComebackALittle(); //皮带后退
            HookMotorStatus = 0;
            ComebackHookToZero();  //钩子复位 
            WaitStatus(HookMotorID, ref HookMotorStatus); //等待钩子复位完成
            CarCarryCupToDetectPositionAndPolePush(0);

            ////皮带后退一点距离
            //BeltComebackALittle();

            //HookMotorStatus = 0;
            //PoleMotorStatus = 0;

            //ResetPushPole(); //复位推杆
            //ResetHook();     //复位钩子

            //WaitStatus(PoleMotorID, ref PoleMotorStatus);//检查推杆是否复位完成

            ////小车复位之前先走一点
            //CarMotorStatus = 0;
            //CarComeBackALittle();
            //WaitStatus(CarMotorID, ref CarMotorStatus);//检查小车是否停止

            //CarMotorStatus = 0;
            //ResetCar();//复位小车

            //WaitStatus(HookMotorID, ref HookMotorStatus);//检查钩子是否复位
            //WaitStatus(CarMotorID, ref CarMotorStatus);//检查小车是否复位


            //Can.SendCan(80, "1IF" + "\r");
            ////小车弹簧松开

            ////Can.SendCan(80, "1VE5" + "\r");
            ////Can.SendCan(80, "1DZ-700" + "\r");
            ////Can.SendCan(80, "1FZ" + "\r");

            //status = 0; //置状态为0 复位完成
        }


        /// <summary>
        /// 清杯操作
        /// </summary>
        public static void DeleteCups()
        {
            CarCarryCupToDetectPositionAndPolePush(1); //小车定位1号条位
                                                       //检查是否定位完成
                                                       //推杯入一号条位
                                                       //检查推杆是否完成
                                                       //推杆复位
                                                       //检查推杆是否完成

            CarMotorStatus = 0;
            HookMotorStatus = 0;
            CarComeBackToReceiveCup(); //小车到对齐点
            HookFirstHalf(); //钩子到等待点

            WaitStatus(CarMotorID, ref CarMotorStatus); //检查小车是否完成
            WaitStatus(HookMotorID, ref HookMotorStatus); //检查钩子是否完成
            HookMotorStatus = 0;
            HookPushCupToCar(); //钩子推杯子上小车
            WaitStatus(HookMotorID, ref HookMotorStatus);  //检查钩子是否完成
            HookMotorStatus = 0;
            ComebackHookToZero(); //钩子复位


            //CarpushCupToDetectPosition(1);
            CarCarryCupToDetectPositionAndPolePush(1);
            //小车定位1号条位
            //检查是否定位完成
            //推杯入一号条位
            //检查推杆是否完成
            //推杆复位
            //检查推杆是否完成
            WaitStatus(HookMotorID, ref HookMotorStatus);//检查钩子是否完成
            status = 1; //置状态为1 清杯完成
        }

        public static void Init()
        {

            StripToWaitInit();
            //皮带前进
            //等待杯子到位信号，超时报警
            //车等待位
            //钩子等待位
            //检查车是否到位
            //检查钩子是否到位
            status = 2; //置状态为2 初始化完成

            while (IsPause)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

        public static void PushMeTo(int stripindex)
        {
            CarMotorStatus = 0;
            CarComeBackToReceiveCup();//车对齐
            WaitStatus(CarMotorID, ref CarMotorStatus);//等待车完成
            HookMotorStatus = 0;
            HookPushCupToCar();//钩子推杯上车
            WaitStatus(HookMotorID, ref HookMotorStatus); //等待钩子到位
            BeltComebackALittle(); //皮带后退
            HookMotorStatus = 0;
            ComebackHookToZero();  //钩子复位 
            WaitStatus(HookMotorID, ref HookMotorStatus); //等待钩子复位完成

            CarCarryCupToDetectPositionAndPolePush(stripindex);
            //CarpushCupToDetectPosition(stripindex);  //小车定位stripindex号条位
            //WaitStatus(CarMotorID, CarMotorStatus);   //检查是否定位完成
            //PushPoleToDetectPosition();   //推杯入stripindex号条位
            //WaitStatus(PoleMotorID, PoleMotorStatus);//检查推杆是否完成
            //PushPoleComeBackZero();     //推杆复位
            //WaitStatus(PoleMotorID, PoleMotorStatus);//检查推杆是否完成



            StripToWait();
            //皮带前进
            //车到等待位
            //检测皮带上是否有杯子到位，超时报警
            //如有杯子，钩子推杯子到等待位
            //检测小车是否到等待位
            //检测钩子是否到等待位

            status = 3; //置状态为3 推杯完成
        }

        public static void CarCarryCupToDetectPositionAndPolePush(int StripIndex)
        {
            CarMotorStatus = 0;
            CarpushCupToDetectPosition(StripIndex);  //小车定位stripindex号条位
            WaitStatus(CarMotorID, ref CarMotorStatus);   //检查是否定位完成
            PoleMotorStatus = 0;
            PushPoleToDetectPosition();   //推杯入stripindex号条位
            WaitStatus(PoleMotorID, ref PoleMotorStatus);//检查推杆是否完成
            PoleMotorStatus = 0;
            PushPoleComeBackZero();     //推杆复位
            WaitStatus(PoleMotorID, ref PoleMotorStatus);//检查推杆是否完成
        }

        public static void StripToWaitInit()
        {

            BeltMotorStatus = 0;
            CarMotorStatus = 0;
            HookMotorStatus = 0;

            GoBelt(); //皮带前进
            ComeToWaitPosition();//车到等待位

            WaitStatus(BeltMotorID, ref BeltMotorStatus);//检测皮带上是否有杯子到位，超时报警

            HookFirstHalf();//如有杯子，钩子推杯子到等待位


            WaitStatus(CarMotorID, ref CarMotorStatus); //检测小车是否到等待位

            WaitStatus(HookMotorID, ref HookMotorStatus); //检测钩子是否到等待位


        }


        public static void StripToWait()
        {

            BeltMotorStatus = 0;
            CarMotorStatus = 0;
            HookMotorStatus = 0;

            GoBelt(); //皮带前进
            ComeToWaitPosition();//车到等待位

            WaitStatus(BeltMotorID, ref BeltMotorStatus);//检测皮带上是否有杯子到位，超时报警

            HookFirstHalf();//如有杯子，钩子推杯子到等待位


            WaitStatus(CarMotorID, ref CarMotorStatus); //检测小车是否到等待位

            WaitStatus(HookMotorID, ref HookMotorStatus); //检测钩子是否到等待位


        }




        public static void BeltComebackALittle()
        {
            Console.WriteLine("皮带回去一点");
            //Can.SendCANData(85, "1VE1" + "\r");
            Can.SendCan(BeltMotorID, "1VE1" + "\r");
            // my_can.SendCANData(85, "1DI-600" + "\r");
            Can.SendCan(BeltMotorID, "1DI-600" + "\r");
            //my_can.SendCANData(85, "1FL" + "\r");
            Can.SendCan(BeltMotorID, "1FL" + "\r");
            //my_can.Check(UartToCAN.Part.FT);
        }

        public static void GoBelt()
        {
            Console.WriteLine("皮带前进");
            //my_can.SendCANData(85, "1AC25" + "\r");
            //my_can.SendCANData(85, "1DE25" + "\r");
            //my_can.SendCANData(85, "1VE1" + "\r");
            //my_can.SendCANData(85, "1DI2" + "\r");
            //my_can.SendCANData(85, "1FS1L" + "\r");
            //my_can.Check(UartToCAN.Part.FT);

            //my_can.SendCANData(85, "1VE1" + "\r");
            //my_can.SendCANData(85, "1DI1000" + "\r");
            //my_can.SendCANData(85, "1FL" + "\r");
            //my_can.Check(UartToCAN.Part.FT);
            Can.SendCan(BeltMotorID, "1AC25" + "\r");
            Can.SendCan(BeltMotorID, "1DE25" + "\r");
            Can.SendCan(BeltMotorID, "1VE0.5" + "\r");
            Can.SendCan(BeltMotorID, "1DI2" + "\r");
            Can.SendCan(BeltMotorID, "1FS1R" + "\r");
            int timeCount = 0;
            while (BeltMotorStatus == 0)
            {
                if (timeCount > 120)
                {
                    BeltMotorStatus = 0;
                    Can.SendCan(BeltMotorID, "1DI0" + "\r");
                    Can.SendCan(BeltMotorID, "1FL" + "\r");
                    WaitStatus(BeltMotorID, ref BeltMotorStatus);
                    System.Windows.Forms.MessageBox.Show("已没有反应杯请您加反应杯！！", "警告！",MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification);
                    //WinShowMessage.ShowDlg("警告", "请输入PT试剂的ISI的值", WinShowMessage.MsgBoxBtns.OK, WinShowMessage.MsgBoxIcon.Information);
                    BeltMotorStatus = 0;
                    //Can.SendCan(BeltMotorID, "1FS1R" + "\r");
                    timeCount = 0;
                }
                timeCount++;
                Thread.Sleep(200);
            }

            WaitStatus(BeltMotorID, ref BeltMotorStatus);

            BeltMotorStatus = 0;
            Can.SendCan(BeltMotorID, "1VE0.5" + "\r");
            Can.SendCan(BeltMotorID, "1DI1000" + "\r");
            Can.SendCan(BeltMotorID, "1FL" + "\r");        
        }

        /// <summary>
        /// 上杯子 把杯子传到后方
        /// </summary>
        public static void CupToSensor()
        {
            BeltMotorStatus = 0;
            Can.SendCan(BeltMotorID, "1FS1R" + "\r");
            WaitStatus(BeltMotorID, ref BeltMotorStatus);
        }


        /// <summary>
        /// 复位钩子
        /// </summary>
        public static void ResetHook()
        {
            Console.WriteLine("复位钩子");
            Can.SendCan(HookMotorID, "1AC25" + "\r");
            Can.SendCan(HookMotorID, "1DE25" + "\r");
            Can.SendCan(HookMotorID, "1VE0.5" + "\r");
            Can.SendCan(HookMotorID, "1DI-100" + "\r");
            Can.SendCan(HookMotorID, "1SH1H" + "\r");

        }
        public static void ComebackHookToZero()
        {
            Console.WriteLine("钩子回到零点！！");
            //my_can.SendCANData(81, "1VE6" + "\r");
            //my_can.SendCANData(81, "1DI0" + "\r");
            //my_can.SendCANData(81, "1FP" + "\r");

            Can.SendCan(HookMotorID, "1VE6" + "\r");
            Can.SendCan(HookMotorID, "1DI0" + "\r");
            Can.SendCan(HookMotorID, "1FP" + "\r");

        }
        public static void CarComeBackALittle()
        {
            Can.SendCan(CarMotorID, "1AC25" + "\r");
            Can.SendCan(CarMotorID, "1DE25" + "\r");
            Can.SendCan(CarMotorID, "1VE1" + "\r");
            Can.SendCan(CarMotorID, "1DI2000" + "\r");
            Can.SendCan(CarMotorID, "1FL" + "\r");

        }


        public static void ResetCar()
        {
            Console.WriteLine("复位小车");
            Can.SendCan(CarMotorID, "1CC4" + "\r");  //更改小车电流2018.1.15
            Can.SendCan(CarMotorID, "1AC25" + "\r");
            Can.SendCan(CarMotorID, "1DE25" + "\r");
            Can.SendCan(CarMotorID, "1VE1" + "\r");
            Can.SendCan(CarMotorID, "1DI-1" + "\r");
            Can.SendCan(CarMotorID, "1SH1H" + "\r");
        }
        public static void PushPoleComeBackZero()
        {
            Console.WriteLine("推杆回到零点");
            //my_can.SendCANData(79, "1VE10" + "\r");
            //my_can.SendCANData(79, "1DI0" + "\r");
            //my_can.SendCANData(79, "1FP" + "\r");

            Can.SendCan(PoleMotorID, "1VE10" + "\r");
            Can.SendCan(PoleMotorID, "1DI0" + "\r");
            Can.SendCan(PoleMotorID, "1FP" + "\r");
        }


        /// <summary>
        /// 复位推杆
        /// </summary>
        public static void ResetPushPole()
        {
            Console.WriteLine("复位推杆");
            Can.SendCan(PoleMotorID, "1AC25" + "\r");
            Can.SendCan(PoleMotorID, "1DE25" + "\r");
            Can.SendCan(PoleMotorID, "1VE1" + "\r");
            Can.SendCan(PoleMotorID, "1DI-10" + "\r");
            Can.SendCan(PoleMotorID, "1SH1H" + "\r");
        }

        /// <summary>
        /// 推杆先走一半
        /// </summary>
        public static void HookFirstHalf()
        {
            Console.WriteLine("推杆先走一半进行等待");
            //my_can.SendCANData(81, "1VE3" + "\r");
            //my_can.SendCANData(81, "1DI7500" + "\r");
            //my_can.SendCANData(81, "1FP" + "\r");

            Can.SendCan(HookMotorID, "1VE3" + "\r");
            Can.SendCan(HookMotorID, "1DI7500" + "\r");
            Can.SendCan(HookMotorID, "1FP" + "\r");
        }

        /// <summary>
        /// 推杆推上小车
        /// </summary>
        public static void HookPushCupToCar()
        {
            Console.WriteLine("推杆推上小车");
            //my_can.SendCANData(81, "1VE1" + "\r");
            //my_can.SendCANData(81, "1DI10" + "\r");
            //my_can.SendCANData(81, "1FS2H" + "\r");

            Can.SendCan(HookMotorID, "1VE1" + "\r");
            Can.SendCan(HookMotorID, "1DI10" + "\r");
            Can.SendCan(HookMotorID, "1FS2H" + "\r");
        }

        /// <summary>
        /// 推杆回到传感器
        /// </summary>
        public static void ComebackPushPole()
        {
            Console.WriteLine("推杆回到传感器！！");
        }

        /// <summary>
        /// 小车回到等待的点
        /// </summary>
        public static void ComeToWaitPosition()
        {
            Console.WriteLine("小车回到等待的点");
            Can.SendCan(CarMotorID, "1VE5" + "\r");
            Can.SendCan(CarMotorID, "1DZ-700" + "\r");
            Can.SendCan(CarMotorID, "1FZ" + "\r");


        }

        /// <summary>
        /// 小车回到接杯子处等待接收杯子
        /// </summary>
        public static void CarComeBackToReceiveCup()
        {
            Console.WriteLine("小车回到了接杯子处接杯子");

            //Can.SendCan(CarMotorID, "1VE5" + "\r");
            //Can.SendCan(CarMotorID, "1DZ-700"+ "\r");
            //Can.SendCan(CarMotorID, "1FZ" + "\r");
            //WaitStatus(CarMotorID, ref CarMotorStatus);//检查小车到距离接杯子处还有500步处

            Can.SendCan(CarMotorID, "1VE1" + "\r");
            Can.SendCan(CarMotorID, "1DZ-1300" + "\r");
            Can.SendCan(CarMotorID, "1FZ" + "\r");
            //my_can.SendCANData(80, "1VE1" + "\r");
            //my_can.SendCANData(80, "1DZ-1300" + "\r");
            //my_can.SendCANData(80, "1FZ" + "\r");



        }

        public static void PushPoleToDetectPosition()
        {
            Console.WriteLine("推杆到推杯子到检测位！！！");
            //my_can.SendCANData(79, "1VE3" + "\r");
            //my_can.SendCANData(79, "1DI43000" + "\r");
            //my_can.SendCANData(79, "1FP" + "\r");

            Can.SendCan(PoleMotorID, "1VE3" + "\r");
            Can.SendCan(PoleMotorID, "1DI43000" + "\r");
            Can.SendCan(PoleMotorID, "1FP" + "\r");

        }

        /// <summary>
        ///小车定位
        /// </summary>
        /// <param name="cupPosition"></param>
        public static void CarpushCupToDetectPosition(int cupPosition)
        {
            Console.WriteLine("杯子推到第" + cupPosition + "个位置！！！！");

            int offSet_Y = FirstDetectPosition + cupPosition * y_OffsetDetect;
            //my_can.SendCANData(80, "1VE3" + "\r");
            //my_can.SendCANData(80, "1DZ" + offSet_Y + "\r");
            //my_can.SendCANData(80, "1FZ" + "\r");

            Can.SendCan(CarMotorID, "1VE3" + "\r");
            Can.SendCan(CarMotorID, "1DZ" + offSet_Y + "\r");
            Can.SendCan(CarMotorID, "1FZ" + "\r");

        }
    }
}
